/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmLogin.java
 *
 * Created on Oct 7, 2011, 10:51:12 PM
 */

package gui;

import Config.Localization;
import bl.BLUser;
import entities.User;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.netbeans.lib.awtextra.AbsoluteConstraints;


/**
 *
 * @author Hung.Scago
 */
public class FrmLogin extends javax.swing.JFrame {

    /** Creates new form FrmLogin */
    User user = null;
    BLUser blUser =null;
    private boolean flag = false;
    private ServerConfig serverConfig;
    private GUIMain main = null;
    
    public FrmLogin() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        this.setLocationRelativeTo(null);
        txtUserName.requestFocus();
        Image img = new ImageIcon(getClass().getResource("/icon/logo1.png")).getImage();
        setIconImage(img);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblIcon = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        lblPassWord = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        txtUserName = new javax.swing.JTextField();
        txtPassWord = new javax.swing.JPasswordField();
        lblTop = new javax.swing.JLabel();
        btnConfig = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login System");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Login Manager.png"))); // NOI18N
        getContentPane().add(lblIcon, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 31, 116, -1));

        lblUserName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUserName.setText("UserName:");
        getContentPane().add(lblUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 60, 70, 21));

        lblPassWord.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblPassWord.setText("PassWord:");
        getContentPane().add(lblPassWord, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 100, 70, -1));

        btnLogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnLogin.setForeground(new java.awt.Color(0, 0, 255));
        btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/key_add.png"))); // NOI18N
        btnLogin.setText("Login");
        btnLogin.setToolTipText("");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        getContentPane().add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 140, -1, 28));

        txtUserName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUserNameActionPerformed(evt);
            }
        });
        getContentPane().add(txtUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 60, 191, -1));

        txtPassWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPassWordActionPerformed(evt);
            }
        });
        txtPassWord.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPassWordKeyPressed(evt);
            }
        });
        getContentPane().add(txtPassWord, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 100, 191, -1));

        lblTop.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblTop.setForeground(new java.awt.Color(51, 0, 255));
        lblTop.setText("LOAN PROCESSING SYSTEM");
        getContentPane().add(lblTop, new org.netbeans.lib.awtextra.AbsoluteConstraints(126, 11, 280, -1));

        btnConfig.setBackground(new java.awt.Color(255, 255, 255));
        btnConfig.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnConfig.setForeground(new java.awt.Color(0, 0, 255));
        btnConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/next.png"))); // NOI18N
        btnConfig.setText("Config");
        btnConfig.setBorder(null);
        btnConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfigActionPerformed(evt);
            }
        });
        getContentPane().add(btnConfig, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 140, 90, 28));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 170, 260, 10));

        btnExit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnExit.setForeground(new java.awt.Color(51, 51, 255));
        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/exit.png"))); // NOI18N
        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        getContentPane().add(btnExit, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 140, -1, 28));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:
        user = new User();
        blUser= new BLUser();
        user.setUserName(txtUserName.getText());
        user.setPassWord(new String(txtPassWord.getPassword()));
        
        try {
            if(blUser.checkLogin(user)){
                System.out.println("xin chao");
                System.out.println("WellCome: " +user.getFullName() );
                System.out.println("Dia chi: " + user.getAddress());
                System.out.println("NGay sinh: " +  user.getBirthDay());
               // dispose();
                this.setVisible(false);
                main = new GUIMain(user);
                main.setVisible(true);

            }
            else{
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("Error2"));
                txtUserName.requestFocus();
               // txtUserName.setText("");
                txtPassWord.setText("");

            }

        } catch (Exception ex) {
            Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void txtPassWordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassWordKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.btnLoginActionPerformed(null);
        }
}//GEN-LAST:event_txtPassWordKeyPressed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, Localization.getInstance().getMessage("Msg2"), "Exit", 2) == 0) {
            System.exit(0);
        }
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigActionPerformed
        // TODO add your handling code here:
        if (flag == false) {
            try {
                if(serverConfig == null){
                    serverConfig = new ServerConfig();
                }
                flag = true;
                //btnConfig.setText("Hide");
                btnConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/review.png")));
                this.getContentPane().add(serverConfig, new AbsoluteConstraints(0, 188, 399, 203));
                serverConfig.setVisible(true);
                pack();
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            flag = false;
            // btnConfig.setText("Show");
            btnConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/next.png")));
            this.remove(serverConfig);
            pack();
        }
}//GEN-LAST:event_btnConfigActionPerformed

    private void txtPassWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPassWordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassWordActionPerformed

    private void txtUserNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUserNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtUserNameActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConfig;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnLogin;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblIcon;
    private javax.swing.JLabel lblPassWord;
    private javax.swing.JLabel lblTop;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JPasswordField txtPassWord;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables

}
